<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2022/9/9
 * Time: 17:25
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class StockoutBo extends Base
{
    /**
     * 出货列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $goods_name = input('goods_name');
            $user_name = input('user_name');
            $bar_code = input('bar_code');
            $fw_code = input('fw_code');
            $where = [];
            if(isset($goods_name) && $goods_name != ''){
                $where['bg_name'] = ['like', '%' . $goods_name . '%'];
            }
            if(isset($user_name) && $user_name != ''){
                $where['user_real_name'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($bar_code) && $bar_code != ''){
                $where['so_bar_code'] = ['like', '%' . $bar_code . '%'];
            }
            if(isset($fw_code) && $fw_code != ''){
                $where['so_fw_code'] = ['LIKE', '%' . $fw_code . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::name('bo_goods_stock_out')
                ->join('up_bo_goods', 'so_goods_id = bg_id', 'LEFT')
                ->join('up_u_user', 'so_user_id = user_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('bo_goods_stock_out')
                ->join('up_bo_goods', 'so_goods_id = bg_id', 'LEFT')
                ->join('up_u_user', 'so_user_id = user_id', 'LEFT')
                ->where($where)
                ->field('so_id, user_username, user_phone, user_shop_name, so_bar_code, so_time, 
                bg_name, so_fw_code, user_real_name')
                ->order('so_time desc')
                ->page($Nowpage,$limits)
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 出货
     * @return mixed|\think\response\Json
     */
    public function stocksOut(){
        if(Request::instance()->isPost()){
            $so_user_id = input('so_user_id');
            $code = input('code');
            if(!$so_user_id) return json(['code' => 100, 'msg' => '请选择出货对象']);
            if(!$code) return json(['code' => 100, 'msg' => '请录入防伪码']);

            $bar_code = explode("\n", $code);
            $bar_code = array_filter($bar_code); // 去掉空数组
            $bar_code = array_unique($bar_code); // 去重

            $data = Db::table('up_bo_goods_stock_info')
                ->join('up_bo_goods_stock', 'gs_id = stock_id', 'LEFT')
                ->where('fw_code', 'IN', $bar_code)
                ->field('goods_id, fw_code, gs_bar_code')
                ->select();
            $fw_code = array_column($data, 'fw_code');

            $insert = [];
            $exist = '';
            foreach($bar_code as $k => $v){
                if(!in_array($v, $fw_code)){
                    $exist .= $v . ',';
                }
                $insert[$k] = [
                    'so_user_id' => $so_user_id,
                    'so_fw_code' => $v,
                    'so_time' => date('Y-m-d H:i:s')
                ];
                foreach ($data as $ke => $vo){
                    if($vo['fw_code'] == $v){
                        $insert[$k]['so_goods_id'] = $vo['goods_id'];
                        $insert[$k]['so_bar_code'] = $vo['gs_bar_code'];
                    }
                }
            }

            if($exist){
                $exist = rtrim($exist, ',');
                return json(['code' => 100, 'msg' => $exist . '防伪码尚未入库']);
            }

            Db::startTrans();
            try {
                // 入出库表
                Db::table('up_bo_goods_stock_out')->insertAll($insert);

                // 从库存列表删除
                Db::table('up_bo_goods_stock_info')->where('fw_code', 'IN', $bar_code)->delete();

                Db::commit();
                return json(['code' => 200, 'msg' => '操作成功']);
            }catch (\Exception $e){
                Db::rollback();
                return json(['code' => 100, 'msg' => $e->getMessage()]);
            }
        }
        $where['user_status'] = 1;
        $where['user_private_worker'] = 1;
        $where['user_rule'] = [['=', 3], ['=', '2,3'], 'or'];
        $user = Db::table('up_u_user')
            ->where($where)
            ->field('user_real_name, user_id, user_shop_name')
            ->select();
        return $this->fetch('', ['user' => $user]);
    }
}